package cn.rose.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author Administrator
 * 获取关键字出现次数算法类
 */
public class GetCount {
	
	public static void main(String[] args) {
		
		Date dateStart = new Date();
		
		
		String file1 = "D:\\javaWorkPlace\\rose-frame\\src\\cn\\uc\\service\\uckey.txt";
		String file2 = "D:\\javaWorkPlace\\rose-frame\\src\\cn\\uc\\service\\sum_citytemplet.txt";
		String file3 = "D:\\javaWorkPlace\\rose-frame\\src\\cn\\uc\\service\\sum_peopletemplet.txt";
		String file4 = "D:\\javaWorkPlace\\rose-frame\\src\\cn\\uc\\service\\count.txt";
		GetCount getCount = new GetCount();
		final List uckeyList = getCount.getListFromFile(file1,"UTF-8");
		final List cityList = getCount.getListFromFile(file2,"GBK");
		final List peopleList = getCount.getListFromFile(file3,"GBK");
		final List countList = getCount.getListFromFile(file4,"UTF-8");
		final HashMap<String,Integer> cityHashMap = new HashMap<String,Integer>();
		final HashMap<String,Integer> peopleHashMap = new HashMap<String,Integer>();
		//开辟线程处理城市匹配
		new Thread(){
			@Override
			public void run() {
				//对比城市
				for(int i = 0; i<cityList.size(); i++){
					String cityStr = cityList.get(i) == null ? "" : cityList.get(i).toString();
					for (int j = 0; j < uckeyList.size(); j++) {
						String uckeyStr = uckeyList.get(j) == null ? "" : uckeyList.get(j).toString();
						if (cityStr.length() == uckeyStr.length() && cityStr.equals(uckeyStr)) {
							cityHashMap.put(cityStr, j);
							break;
						}
					}
				}
				Set citySet = cityHashMap.keySet();
				List<KeyWordModel> cityKeyWorldList = new ArrayList<KeyWordModel>();
				KeyWordModel keyWordModel = null;
				for (Iterator iterator = citySet.iterator(); iterator.hasNext();) {
					String keyword = iterator.next().toString();
					int index = cityHashMap.get(keyword);
					int count = new Integer(countList.get(index).toString());
					System.out.println(keyword + " >>> " + count);
					keyWordModel = new KeyWordModel();
					keyWordModel.setHdKeyWord(keyword);
					keyWordModel.setUcKeyWord(keyword);
					keyWordModel.setCount(count);
					cityKeyWorldList.add(keyWordModel);
				}
				Collections.sort(cityKeyWorldList);
				//输出excele
				ExportExcel<KeyWordModel> excel = new ExportExcel<KeyWordModel>();
				String[] strs = {"互动百科关键字","UC关键字","检索次数"};
				try {
					excel.exportExcel("互动百科城市匹配",strs, cityKeyWorldList, new FileOutputStream("F://互动百科城市匹配thread.xls"));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}.start();
		
		
		
		//开辟线程处理人物匹配
		new Thread(){
			@Override
			public void run() {
				//对比人物
				for(int i = 0; i<peopleList.size(); i++){
					String peopleStr = peopleList.get(i) == null ? "" : peopleList.get(i).toString();
					for (int j = 0; j < uckeyList.size(); j++) {
						String uckeyStr = uckeyList.get(j) == null ? "" : uckeyList.get(j).toString();
						if (peopleStr.length() == uckeyStr.length() && peopleStr.equals(uckeyStr)) {
							peopleHashMap.put(peopleStr, j);
							break;
						}
					}
				}
				List<KeyWordModel> peopleKeyWorldList = new ArrayList<KeyWordModel>();
				Set peopleSet = peopleHashMap.keySet();
				KeyWordModel keyWordModel = null;
				for (Iterator iterator = peopleSet.iterator(); iterator.hasNext();) {
					String keyword = iterator.next().toString();
					int index = peopleHashMap.get(keyword);
					int count = new Integer(countList.get(index).toString());
					System.out.println(keyword + " >>> " + count);
					keyWordModel = new KeyWordModel();
					keyWordModel.setHdKeyWord(keyword);
					keyWordModel.setUcKeyWord(keyword);
					keyWordModel.setCount(count);
					peopleKeyWorldList.add(keyWordModel);
				}
				Collections.sort(peopleKeyWorldList);
				//输出excele
				ExportExcel<KeyWordModel> excel = new ExportExcel<KeyWordModel>();
				String[] strs = {"互动百科关键字","UC关键字","检索次数"};
				try {
					excel.exportExcel("互动百科人物匹配",strs, peopleKeyWorldList, new FileOutputStream("F://互动百科人物匹配thread.xls"));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
			}
		}.start();
	}
	/**
	 * 用集合封装文件内容，以行为单位
	 * @param fileName
	 * @return
	 * List
	 * @author rose
	 */
	public List getListFromFile(String fileName,String encoding){
		List list = new ArrayList();
		InputStream is = null;
		LineNumberReader lnr = null;
		try {
			is = new FileInputStream(fileName);
			lnr = new LineNumberReader(new InputStreamReader(is,encoding));
			String temp = "";
			while((temp = lnr.readLine()) != null){
				list.add(temp);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
